package com.common.autocode;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.map.MultiKeyMap;
import org.apache.commons.io.output.FileWriterWithEncoding;

import com.common.annotation.Column;
import com.common.annotation.ID;
import com.common.annotation.Table;

public class EntityAnalysis {
	private static final String encoding = "utf-8";
	private static Map types = new HashMap();
	static{
		types.put("java.lang.String", "VARCHAR");
		types.put("java.util.Date", "TIMESTAMP");
		types.put("java.lang.Integer", "INTEGER");
		types.put("java.lang.Double", "DOUBLE");
	}
	
	private static MultiKeyMap multMap = new MultiKeyMap();
	
	public static void analysis(String classname){
		try {
			StringBuffer buffer = new StringBuffer("create table ");
			Class clazz = Class.forName(classname);
			String tablename = clazz.getSimpleName();
			Annotation ann = clazz.getAnnotation(Table.class);
			if(ann != null){
				BeanInfo info = new BeanInfo();
				Table table = (Table)ann;
				if("".equals(table.val())){
					tablename = "t_"+tablename;
					buffer.append(tablename);
				}else{
					tablename = table.val();
					buffer.append(tablename);
				}
			}
			buffer.append("(");
			Field[] fields = clazz.getDeclaredFields();
			for(Field field : fields){
				ID id = field.getAnnotation(ID.class);
				if(id != null){
					if("".equals(id.val())){
						buffer.append(field.getName()+" "+types.get(field.getType().getName()));
					}else{
						buffer.append(id.val());
					}
					if(id.auto()){
						buffer.append(" AUTO_INCREMENT");
					}
					buffer.append(" PRIMARY KEY,");
				}
				
				Column column = field.getAnnotation(Column.class);
				if(column != null){
					if("".equals(column.val())){
						buffer.append(field.getName()+" "+types.get(field.getType().getName()));
					}else{
						buffer.append(column.val()+" "+types.get(field.getType().getName()));
					}
					if(column.isnull()){
						buffer.append(" NOT NULL");
					}
					buffer.append(",");
				}
				multMap.put(field.getName(), field.getType().getName(),types.get(field.getType().getName()));
			}
			buffer.setLength(buffer.length()-1);
			buffer.append(");");
			
			String basePath = "src/"+clazz.getPackage().getName().replaceAll("\\.", "/");
			
			//create sql
			String ddlPath = basePath+"/ddl";
			String ddlFile = tablename+".sql";
			writeFile(ddlPath,ddlFile,buffer.toString());
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void writeFile(String path,String name,String msg) throws IOException{
		createFile(path);
		File file = new File(path+"/"+name);
		FileWriterWithEncoding writer = new FileWriterWithEncoding(file,encoding);
		writer.write(msg);
		writer.close();
	}
	
	private static void createFile(String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
	}
	
	public static void main(String[] args) throws Exception {
		analysis("com.common.autocode.test.Test");
	}
}
